package com.tjetc.controller;


import com.tjetc.common.JsonResult;
import com.tjetc.entity.Order;
import com.tjetc.entity.OrderDetail;
import com.tjetc.entity.OrderMap;
import com.tjetc.service.Impl.SnowflakeIdWorkerService;
import com.tjetc.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /***
     * 管理员端添加订单
     * @param order
     * @return
     */
    @RequestMapping("add")
    public JsonResult addOrder(Order order) {
        //提交订单时，未包含userId、订单编号、下单时间
        int i = orderService.add(order);
        if (i > 0) {
            return new JsonResult(0, "添加成功", true);
        } else {
            return new JsonResult(1, "添加失败", false);
        }
    }

    /***
     * 用户添加订单并执行入库相关操作
     * @param orderMap
     * @return
     */
    @RequestMapping("addToOrder")
    public JsonResult addToOrder(@RequestBody OrderMap orderMap) {
        //提交订单时，未包含userId、订单编号、下单时间
        Order order=orderMap.getOrder();
        List <Integer> list=orderMap.getList();
        int i = orderService.addToOrder(order,list);
        if (i > 0) {
            return new JsonResult(0, "添加成功", i);
        } else {
            return new JsonResult(1, "添加失败", false);
        }
    }


    /***
     * 订单id查询订单
     * @param id
     * @return
     */
    @RequestMapping("query/{id}")
    public JsonResult queryOrderByOrderId(@PathVariable int id) {
        Order order = orderService.selectByOrderId(id);
        if (order != null) {
            return new JsonResult(0, "查询成功", order);
        } else return new JsonResult(1, "查询失败", null);
    }

    /***
     * 用户id查询用户所有订单
     * @param id
     * @return
     */
    @RequestMapping("all/{id}")
    public JsonResult queryOrders(@PathVariable int id) {
        List<Order> orders = orderService.selectByUserId(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询用户待支付订单
     * @param id
     * @return
     */
    @RequestMapping("waitpay/{id}")
    public JsonResult queryWaitPay(@PathVariable int id) {
        List<Order> orders = orderService.selectWaitPay(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询用户待送达订单
     * @param id
     * @return
     */
    @RequestMapping("waitarrive/{id}")
    public JsonResult queryWaitArrive(@PathVariable int id) {
        List<Order> orders = orderService.selectWaitArrive(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询用户待评论订单
     * @param id
     * @return
     */
    @RequestMapping("waitremark/{id}")
    public JsonResult queryWaitRemark(@PathVariable int id) {
        List<Order> orders = orderService.selectWaitRemark(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询用户待退款或已申请退款的订单
     * @param id
     * @return
     */
    @RequestMapping("afterpay/{id}")
    public JsonResult queryAfterPay(@PathVariable int id) {
        List<Order> orders = orderService.selectAfterPay(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询用户未完成的订单
     * @param id
     * @return
     */
    @RequestMapping("unaccomplished/{id}")
    public JsonResult queryUnaccomplished(@PathVariable int id) {
        List<Order> orders = orderService.selectUnaccomplished(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 查询申请退款的订单
     * @param id
     * @return
     */
    @RequestMapping("refund/{id}")
    public JsonResult queryRefund(@PathVariable int id) {
        List<Order> orders = orderService.selectRefund(id);
        if (orders != null) {
            return new JsonResult(0, "查询成功", orders);
        } else {
            return new JsonResult(1, "查询失败", orders);
        }
    }

    /***
     * 根据商家id查询商家所有订单
     * @param id
     * @return
     */
    @RequestMapping("allorders/{id}")
    public JsonResult queryAllOrders(@PathVariable int id) {
        List<Order> orderList = orderService.selectAllByMerchantId(id);
        if (orderList != null) {
            return new JsonResult(0, "所有订单查询成功", orderList);
        } else {
            return new JsonResult(1, "查询失败", orderList);
        }
    }

    /***
     * 删除订单
     * @param orderId
     * @return
     */
    @RequestMapping("delete/{orderId}")
    public JsonResult delete(@PathVariable int orderId) {
        int delete = orderService.delete(orderId);
        if (delete <= 0) {
            return new JsonResult(1, "订单错误", false);
        } else {
            return new JsonResult(0, "删除成功", true);
        }
    }

    /***
     * 修改订单状态
     * @param id
     * @param status
     * @return
     */
    @RequestMapping("changeStatus")
    public JsonResult changerStatus(Integer id,Integer status){
        int i =orderService.changeOrderStatus(id,status);
        return new JsonResult(0,"修改成功",i);
    }

}